package com.hdl.servlet.user;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hdl.entity.HdlUser;
import com.hdl.service.UserService;
import com.hdl.service.impl.UserServiceImpl;
import com.hdl.util.MailUtil;
import com.hdl.util.SysUtil;
import com.hdl.util.UploadLogUtil;

/**
 * 实现注册加头像上传
 * @author Administrator
 *
 */
@WebServlet("/regServlet")
public class RegServlet1 extends HttpServlet{
	
	private UserService service = new UserServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//创建对象
		HdlUser user = new HdlUser();
		
		//保存头像 封装属性
		UploadLogUtil.upload(req, resp, user);
		
		//添加系统属性
		user.setSysRoleId(49);//普通会员
		user.setHdlUserActiveCode(SysUtil.get());//用户激活码
		user.setHdlUserId(SysUtil.get());//用户id
		user.setHdlUserStatus(1);//用户默认锁定状态
		user.setHdlUserScore(0);//初始积分为0
		
		
		//调用用户业务逻辑层方法
		service.register(user);
		
		//通知用户激活账号
		try {
			MailUtil.sendQQ(user.getHdlUserEmail(), user.getHdlUserActiveCode());
		} catch (MessagingException e) {
			//日志打印
			e.printStackTrace();
		}
		
		
		//转发页面
		req.setAttribute("msg", "请查看邮箱，使用激活码激活账号!");//转发消息
		req.getRequestDispatcher("/msg.jsp").forward(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
